﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="ColorExtensions.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2010 
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.Core.
//
//   starLiGHT.Core is free software: you can redistribute it and/or modify
//   it under the terms of the GNU Lesser General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//
//   starLiGHT.Core is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU Lesser General Public License for more details.
//
//   You should have received a copy of the GNU Lesser General Public License
//   along with starLiGHT.Core. If not, see http://www.gnu.org/licenses/.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.Core are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev:: 620                   $:  Revision of last commit *
// * $Author:: unknown            $:  Author of last commit   *
// * $Date:: 2010-12-07 15:23:58 #$:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT
{
    #region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;

    #endregion

    /// <summary>
    /// Extension methods for the Color data type
    /// </summary>
    public static class ColorExtensions
    {
        // Methods
        public static Color Premultiply(this Color color)
        {
            return new Color(
                             (byte)((color.R * color.A) / 255), 
                             (byte)((color.G * color.A) / 255), 
                             (byte)((color.B * color.A) / 255), 
                             color.A);
        }

        public static Color SetAlpha(this Color color, byte newValue)
        {
            return new Color(color.R, color.G, color.B, newValue);
        }

#if !XBOX && !ZUNE && !WINDOWS_PHONE
        public static Color FromString(string colorstring)
        {
            colorstring = colorstring.Replace("{", string.Empty).Replace("}", string.Empty);
            string[] colParts = colorstring.Split(new string[] { " ", ":" }, StringSplitOptions.RemoveEmptyEntries);

            byte r = 0, g = 0, b = 0, a = 0;
            if (byte.TryParse(colParts[1], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out r) &&
                byte.TryParse(colParts[3], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out g) &&
                byte.TryParse(colParts[5], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out b) &&
                byte.TryParse(colParts[7], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out a))
            {
                return new Color(r, g, b, a);
            }
            else
            {
                throw new FormatException(string.Format("Couldn't parse the color string: {0}", colorstring));
            }
        }
#else
        public static Color FromString(string colorstring)
        {
            colorstring = colorstring.Replace("{", string.Empty).Replace("}", string.Empty);
            string[] colParts = colorstring.Split(new char[] { ' ', ':' });

            byte r = 0, g = 0, b = 0, a = 0;
            if (ByteTryParse(colParts[1], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out r) &&
                ByteTryParse(colParts[3], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out g) &&
                ByteTryParse(colParts[5], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out b) &&
                ByteTryParse(colParts[7], System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.InvariantCulture, out a))
            {
                return new Color(r, g, b, a);
            }
            else
            {
                throw new FormatException(string.Format("Couldn't parse the color string: {0}", colorstring));
            }
        }

        private static bool ByteTryParse(string s, System.Globalization.NumberStyles style, IFormatProvider provider, out byte b)
        {
            try
            {
                b = byte.Parse(s, style, provider);
                return true;
            }
            catch (Exception ex)
            {
                b = 0;
                return false;
            }
        }
#endif
    }
}
